package matlap.client.view.admin;

import java.util.Iterator;
import java.util.List;

import matlap.client.services.distributor.DistributorService;
import matlap.client.services.distributor.DistributorServiceAsync;
import matlap.client.services.registration.RegService;
import matlap.client.services.registration.RegServiceAsync;
import matlap.shared.Distributor;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DistributorsComposite extends Composite {

	DistributorServiceAsync d1 = GWT.create(DistributorService.class);
	RegServiceAsync r1 = GWT.create(RegService.class);

	private VerticalPanel vp;
	VerticalPanel simplePanel = new VerticalPanel();
	Panel newPanel = new HorizontalPanel();
	Panel updatePanel = new VerticalPanel();
	Panel deletePanel = new VerticalPanel();
	Panel buttonPanel = new HorizontalPanel();
	Panel gridPanel = new VerticalPanel();
	private Label lbName;
	private TextBox tbName;
	private Button btnSearch;
	private Grid g;
	protected String name;
	ListBox who1comboBox;
	ListBox who2comboBox;
	Button newButton;
	Button updateButton;
	Button deleteButton;

	public DistributorsComposite() {

		vp = new VerticalPanel();
		initWidget(vp);
		vp.setSize("670px", "500px");

		initButtonPanel();
		vp.add(buttonPanel);
		buttonPanel.setWidth("686px");
		updateButtonColor(1, 0, 0);
		initNewPanel();
		initUpdatePanel();
		initDeletePanel();
		simplePanel.add(newPanel);
		simplePanel.setBorderWidth(1);
		vp.add(simplePanel);
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		vp.add(decoratorPanel);

		Label lblNewLabel_2 = new Label("A jelenlegi rendel\u0151k:");
		lblNewLabel_2.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_2);

		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setBorderWidth(1);
		vp.add(verticalPanel);
		verticalPanel.setSize("684px", "68px");

		FlexTable filterFlex = new FlexTable();
		verticalPanel.add(filterFlex);
		filterFlex.setSize("688px", "39px");

		lbName = new Label("N\u00E9v");
		filterFlex.setWidget(0, 0, lbName);

		tbName = new TextBox();
		filterFlex.setWidget(1, 0, tbName);

		btnSearch = new Button("New button");
		btnSearch.setText("Keres");
		filterFlex.setWidget(1, 1, btnSearch);

		btnSearch.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				name = tbName.getText();

				d1.getDistributorsByName(name,
						new AsyncCallback<List<Distributor>>() {

					@Override
					public void onFailure(Throwable caught) {

					}

					@Override
					public void onSuccess(List<Distributor> result) {
						int i = 1;
						Iterator<Distributor> it = result.iterator();
						g.resize(result.size() + 1, 3);
						while (it.hasNext()) {
							Distributor teach = (Distributor) it.next();

							g.setText(i, 0, teach.getLastName() + " "
									+ teach.getFirstName());

							g.setText(i, 1, teach.getUserName());
							g.setText(i, 2, teach.getAddress());
							i++;
							i++;

						}

					}
				});
			}
		});

		initGrid();
	}

	public void updateButtonColor(int c1, int c2, int c3) {
		if (c1 == 0) {
			newButton.setStyleName("Button1");
		} else {
			newButton.setStyleName("Button2");
		}
		if (c2 == 0) {
			updateButton.setStyleName("Button1");
		} else {
			updateButton.setStyleName("Button2");
		}

		if (c3 == 0) {
			deleteButton.setStyleName("Button1");
		} else {
			deleteButton.setStyleName("Button2");
		}

	}

	public void initGrid() {
		g = new Grid(1, 3);

		Label lbNev = new Label("N\u00E9v");
		lbNev.setStyleName("Header");
		g.setWidget(0, 0, lbNev);

		Label lbFelh = new Label("Felhaszn\u00E1l\u00F3n\u00E9v");
		lbFelh.setStyleName("Header");
		g.setWidget(0, 1, lbFelh);

		Label lbCim = new Label("C\u00EDm");
		lbCim.setStyleName("Header");
		g.setWidget(0, 2, lbCim);

		g.setBorderWidth(2);
		gridPanel.add(g);
		vp.add(gridPanel);
		gridPanel.setSize("684px", "34px");
		g.setSize("688px", "34px");

		d1.getDistributorsNumber(new AsyncCallback<Integer>() {

			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			public void onSuccess(Integer result) {
				g.resize(result + 1, 3);
			}
		});



		d1.getDistributors(new AsyncCallback<List<Distributor>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond van people");


			}

			@Override
			public void onSuccess(List<Distributor> result) {
				int i = 1;
				Iterator<Distributor> it = result.iterator();
				while (it.hasNext()) {
					Distributor distr = (Distributor) it.next();

					g.setText(i, 0, distr.getLastName() + " " + distr.getFirstName());
					g.setText(i, 1, distr.getUserName());
					g.setText(i, 2, distr.getAddress());
					i++;

				}

			}

		});
	}

	public void updateUserNames() {
		who1comboBox.clear();
		who2comboBox.clear();
		who1comboBox.addItem("");
		who2comboBox.addItem("");
		d1.getUserNames(new AsyncCallback<List<String>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond");

			}

			@Override
			public void onSuccess(List<String> result) {
				Iterator<String> it = result.iterator();
				while (it.hasNext()) {
					String name = (String) it.next();
					who1comboBox.addItem(name);
					who2comboBox.addItem(name);
				}

			}

		});

	}

	public void initButtonPanel() {
		newButton = new Button("\u00DAj rendel\u0151");
		newButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent sender) {
				updateButtonColor(1, 0, 0);
				simplePanel.clear();
				simplePanel.add(newPanel);
			}
		});
		buttonPanel.add(newButton);
		newButton.setWidth("226px");

		updateButton = new Button("Rendel\u0151 m\u00F3dos\u00EDt\u00E1sa");
		updateButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent sender) {
				updateButtonColor(0, 1, 0);
				simplePanel.clear();
				simplePanel.add(updatePanel);
			}
		});
		buttonPanel.add(updateButton);
		updateButton.setWidth("220px");

		deleteButton = new Button("Rendel\u0151 t\u00F6rl\u00E9se");
		deleteButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent sender) {
				updateButtonColor(0, 0, 1);
				simplePanel.clear();
				simplePanel.add(deletePanel);
			}
		});
		buttonPanel.add(deleteButton);
		deleteButton.setWidth("240px");

	}

	public void initNewPanel() {
		FlexTable fl = new FlexTable();
		newPanel.add(fl);
		fl.setSize("684px", "38px");

		Label vnLabel = new Label("Vezet\u00E9kn\u00E9v");
		fl.setWidget(0, 0, vnLabel);
		vnLabel.setWidth("225px");
		Label cimLabel = new Label("Lakc\u00EDm(Helys\u00E9g, Utca, Sz\u00E1m)");
		fl.setWidget(0, 1, cimLabel);

		final TextBox vn1textBox = new TextBox();
		vn1textBox.setTabIndex(1);
		fl.setWidget(1, 0, vn1textBox);

		final TextBox address1textBox = new TextBox();
		fl.setWidget(1, 1, address1textBox);
		address1textBox.setTabIndex(3);

		Label knLabel = new Label("Keresztn\u00E9v");
		fl.setWidget(2, 0, knLabel);

		final TextBox kn1textBox = new TextBox();
		fl.setWidget(3, 0, kn1textBox);
		kn1textBox.setTabIndex(2);

		Button btnSave1 = new Button("Ment\u00E9s");
		btnSave1.setTabIndex(4);
		fl.setWidget(3, 1, btnSave1);
		fl.getCellFormatter().setHorizontalAlignment(3, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
		btnSave1.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				String lname = vn1textBox.getText();
				String fname = kn1textBox.getText();
				String address = address1textBox.getText();

				if ((fname.length() == 0) || (lname.length() == 0)
						|| (address.length() == 0)) {
					Window.alert("Nem adott meg minden adatot!");
				} else {
					vn1textBox.setText("");
					kn1textBox.setText("");
					address1textBox.setText("");
					r1.insertInPeople(3, lname, fname, address,
							new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Gond");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							gridPanel.clear();
							initGrid();
							updateUserNames();
						}

					});
				}
			}

		});
	}

	public void initUpdatePanel() {
		FlexTable fl2 = new FlexTable();
		updatePanel.add(fl2);
		fl2.setSize("677px", "38px");

		Label lblNewLabel = new Label(
				"A m\u00F3dos\u00EDtand\u00F3 szem\u00E9ly:");
		fl2.setWidget(0, 0, lblNewLabel);

		Label vn2Label = new Label("Vezet\u00E9kn\u00E9v");
		fl2.setWidget(0, 2, vn2Label);
		Label cim2Label = new Label("C\u00EDm");
		fl2.setWidget(0, 4, cim2Label);

		final TextBox vn2textBox = new TextBox();
		fl2.setWidget(1, 2, vn2textBox);
		vn2textBox.setTabIndex(2);
		final TextBox address2textBox_2 = new TextBox();
		fl2.setWidget(1, 4, address2textBox_2);
		address2textBox_2.setTabIndex(4);
		Label kn2Label = new Label("Keresztn\u00E9v");
		fl2.setWidget(2, 2, kn2Label);

		final TextBox kn2textBox_1 = new TextBox();
		fl2.setWidget(3, 2, kn2textBox_1);
		kn2textBox_1.setTabIndex(3);
		who1comboBox = new ListBox();
		who1comboBox.setTabIndex(1);
		fl2.setWidget(1, 0, who1comboBox);
		who1comboBox.addItem("");
		d1.getUserNames(new AsyncCallback<List<String>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond");

			}

			@Override
			public void onSuccess(List<String> result) {
				Iterator<String> it = result.iterator();
				while (it.hasNext()) {
					who1comboBox.addItem((String) it.next());
				}

			}

		});
		fl2.getCellFormatter().setHorizontalAlignment(3, 4,
				HasHorizontalAlignment.ALIGN_LEFT);
		who1comboBox.addChangeHandler(new ChangeHandler() {

			public void onChange(ChangeEvent sender) {
				String user = who1comboBox.getValue(who1comboBox
						.getSelectedIndex());
				d1.getDistributorsByUserName(user,
						new AsyncCallback<Distributor>() {

					public void onFailure(Throwable caught) {
						Window.alert("Hiba");

					}

					@Override
					public void onSuccess(Distributor result) {

						vn2textBox.setText(result.getLastName());
						kn2textBox_1.setText(result.getFirstName());
						address2textBox_2.setText(result.getAddress());

					}

				});

			}

		});

		Button btnSave2 = new Button("Ment\u00E9s");
		fl2.setWidget(3, 4, btnSave2);
		btnSave2.setTabIndex(5);
		btnSave2.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				String who = who1comboBox.getItemText(who1comboBox
						.getSelectedIndex());
				String lname = vn2textBox.getText();
				String fname = kn2textBox_1.getText();
				String address = address2textBox_2.getText();

				if ((who.length() == 0) || (fname.length() == 0)
						|| (lname.length() == 0) || (address.length() == 0)) {
					Window.alert("Nem adott meg minden adatot!");
				} else {
					who1comboBox.setSelectedIndex(0);
					vn2textBox.setText("");
					kn2textBox_1.setText("");
					address2textBox_2.setText("");
					d1.updateDistributors(who, fname, lname, address,
							new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Gond");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							gridPanel.clear();
							initGrid();

						}

					});
				}
			}

		});

		who1comboBox.addChangeHandler(new ChangeHandler() {

			public void onChange(ChangeEvent sender) {
				who1comboBox.getValue(who1comboBox
						.getSelectedIndex());

			}

		});
	}

	public void initDeletePanel() {
		FlexTable fl3 = new FlexTable();
		deletePanel.add(fl3);
		fl3.setSize("676px", "38px");

		Label who2Label = new Label("Kit akar t\u00F6r\u00F6lni?");
		fl3.setWidget(0, 2, who2Label);
		who2Label.setWidth("225px");

		who2comboBox = new ListBox();
		fl3.setWidget(2, 2, who2comboBox);
		who2comboBox.addItem("");
		d1.getUserNames(new AsyncCallback<List<String>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond");

			}

			@Override
			public void onSuccess(List<String> result) {
				Iterator<String> it = result.iterator();
				while (it.hasNext()) {
					who2comboBox.addItem((String) it.next());
				}

			}

		});

		Button btnDelete = new Button("T\u00F6rl\u00E9s");
		fl3.setWidget(2, 3, btnDelete);
		fl3.getCellFormatter().setHorizontalAlignment(2, 3,
				HasHorizontalAlignment.ALIGN_RIGHT);
		btnDelete.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String who = who2comboBox.getItemText(who2comboBox
						.getSelectedIndex());
				who2comboBox.setSelectedIndex(0);
				if (who.length() == 0) {
					Window.alert("Nem valasztott ki senkit!");
				} else {
					d1.deleteDistributors(who, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Gond");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							gridPanel.clear();
							initGrid();
							updateUserNames();
						}

					});
				}

			}
		});
	}

}
